/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.plugins;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.entity.Dictionary;
import org.grayrabbit.cms.entity.SubjectOption;
import org.grayrabbit.cms.entity.Survey;
import org.grayrabbit.cms.entity.SurveySubject;
import org.grayrabbit.cms.service.dictionary.DictionaryService;
import org.grayrabbit.cms.service.plugins.SubjectOptionService;
import org.grayrabbit.cms.service.plugins.SurveySubjectService;
import org.grayrabbit.cms.vo.plugins.SurveySubjectBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.web.struts2.GenericAction;

/**
 * <b>SurveySubjectAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-05-16 16:08:41 
 */
@Controller
@Scope("prototype")
@ResultPath("/WEB-INF/content/plugins")
public class SurveySubjectAction extends GenericAction<SurveySubjectBean> {
	private static final long serialVersionUID = -1L;
	public static final String SURVEY = "survey";
	//Service接口接入
	@Autowired
	@Qualifier("surveySubjectServiceImpl")
	private SurveySubjectService surveySubjectService;
	@Autowired
	@Qualifier("subjectOptionServiceImpl")
	private SubjectOptionService subjectOptionService;
	//VO对象，由前端页面数据组装
	private SurveySubjectBean surveySubject;
	
	private Survey survey;
	
	private String flag;
	
	private String surveyId;
	
	@Autowired
	@Qualifier("dictionaryServiceImpl")
	private DictionaryService dictionaryService;
	
	private List<Dictionary> dicSurveyCheckType;
	
	public List<Dictionary> getDicSurveyCheckType() {
		return dicSurveyCheckType;
	}

	public void setDicSurveyCheckType(List<Dictionary> dicSurveyCheckType) {
		this.dicSurveyCheckType = dicSurveyCheckType;
	}
	
	/**return 前端页面数据组装VO对象,<b>SurveySubjectBean</b>*/
	public SurveySubjectBean getSurveySubject() {
		return surveySubject;
	}

	public void setSurveySubject(SurveySubjectBean surveySubject) {
		this.surveySubject = surveySubject;
	}

	public Survey getSurvey() {
		return survey;
	}

	public void setSurvey(Survey survey) {
		this.survey = survey;
	}
	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	public String getSurveyId() {
		return surveyId;
	}

	public void setSurveyId(String surveyId) {
		this.surveyId = surveyId;
	}
	//分页对象
	private Page<SurveySubject> page = new Page<SurveySubject>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<SurveySubject> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<SurveySubject> page) {
		this.page = page;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(surveySubject==null){
			surveySubject = new SurveySubjectBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public SurveySubjectBean getModel() {
		return surveySubject;
	}
	
	@Override
	protected void prepareModel(){
		if(StringUtils.isNotBlank(surveySubject.getSubjectId())){
			SurveySubject item = surveySubjectService.getSurveySubject(surveySubject.getSubjectId());
			surveySubject = (SurveySubjectBean)dozer.map(surveySubjectService.getSurveySubject(surveySubject.getSubjectId()),SurveySubjectBean.class);
//			surveySubject.setSurvey(new Survey(survey.getSurveyId()));
			surveySubject.setSurvey(item.getSurvey());
		}
	}

	/**
	 * 删除<b>SurveySubject</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到surveysubject-list.action
	 */
	@Override
	@Action(value="surveysubject-delete",
			results={@Result(name="reload",type="redirectAction",params={"actionName","surveysubject-list"})}
			)
	public String delete(){
		surveySubjectService.delete(key);
		return RELOAD;
	}
	@Action(value="surveysubject-del")
	public String del(){
		List<SubjectOption> list = subjectOptionService.findBySubjectId(key[0]);
		String[] optIds = new String[list.size()];
		for(int i=0;i<list.size();i++){
			optIds[i] = list.get(i).getOptionId();
		}
			subjectOptionService.delete(optIds);
			surveySubjectService.delete(key);
		return null;
	}
	/**
	 * SurveySubject的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到surveysubject-list.jsp页面
	 */
	@Override
	@Action(value="surveysubject-list")
	public String list(){
		surveySubject.setSurvey(new Survey(survey.getSurveyId()));
		surveySubjectService.list(page,surveySubject);
		return SUCCESS;
	}
	/**
	 * 根据调查主题id查询调查科目
	 * @return
	 */
	@Action(value="surveysubject-allList")
	public String allList(){
		String text = "";
		surveyId = survey.getSurveyId();
		List<SurveySubject> list = surveySubjectService.findBySurveyId(surveyId);
		if(list.size()>0){
			text = "yes";
		}else{
			text = "no";
		}
		return renderText(text);
	}
	/**
	 * SurveySubject的新增与编辑的方法执行成功能
	 * 将映射到surveysubject-input.jsp
	 */
	@Action(value="surveysubject-input")
	public String input(){
		if(survey != null){
			surveyId = survey.getSurveyId();
		}
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTECHECKTYPE)!=null){
			dicSurveyCheckType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.VOTECHECKTYPE);
		}
		prepareModel();
		return SUCCESS;
	}
	
	/**
	 * save or update SurveySubject的方法，将成员property surveySubject转成SurveySubject实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到surveysubject-list.action
	 */
	@Override
	@Action(value="surveysubject-save",
			results={@Result(name="reload",type="redirectAction",params={"actionName","survey-list"}),
					 @Result(name="survey",type="redirectAction",params={"actionName","surveysubject-input"})}
		)
	public String save(){
		SurveySubject obj = (SurveySubject) dozer.map(surveySubject, SurveySubject.class);
		if(!StringUtils.isNotBlank(obj.getSubjectId())){
			obj.setSubjectId(null);
		}
		obj.setSurvey(new Survey(surveyId));
		surveySubjectService.save(obj);
		
		if(flag.equals("sur")){
			return SURVEY;
		}else{
			return RELOAD;
		}
	}
	
	/**
	 * 根据科目Id查询
	 * @return
	 */
	@Action(value="surveysubject-getSub")
	public String findBySubjectId(){
		List<SurveySubject> list = surveySubjectService.findBySubId(surveySubject.getSubjectId());
		Map<String,Object> map =new HashMap<String,Object>(); 
		 JSONObject json = new  JSONObject();
		 JSONArray jsonArray = new JSONArray();
		for(int i=0;i<list.size();i++){
			map.put("subId", list.get(i).getSubjectId());
			map.put("subjectTitle", list.get(i).getSubjectTitle());
			map.put("checkModel", list.get(i).getSubjectCheckModel());
			json = JSONObject.fromObject( map ); 
			jsonArray.add(json);
		}
		return renderText(jsonArray.toString());
	}
}